/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fx.reports.beerCharacteristics;

import beans.Avaliation;
import beans.Beer;
import dao.DaoAvaliation;
import dao.DaoBeer;
import fx.Home;
import fx.judge.JudgeController;
import fx.menu.MenuController;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 * FXML Controller class
 *
 * @author Jandrei
 */
public class BeerCharacteristicsController implements Initializable {

    Home application;
    Beer beer;
    DaoBeer daoBeer;
    DaoAvaliation daoAvaliation;

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private BarChart<?, ?> barChart;

    @FXML
    private Button btnCancel;

//    private XYChart.Series<String, Integer> series;
    @FXML
    void initialize() {
        assert barChart != null : "fx:id=\"barChart\" was not injected: check your FXML file 'beerCharacteristics.fxml'.";
        assert btnCancel != null : "fx:id=\"btnCancel\" was not injected: check your FXML file 'beerCharacteristics.fxml'.";
    }

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        barChart.setTitle("Report by Feature Beer");
//        series = new XYChart.Series<>();
        daoBeer = new DaoBeer();
        daoAvaliation = new DaoAvaliation();
        List<Beer> listBeers = daoBeer.getAll();
//        Map<Beer, List<Integer>> map = new HashMap<Beer, List<Integer>>();
        XYChart.Series seriesAroma = new XYChart.Series();
        seriesAroma.setName("Aroma");
        XYChart.Series seriesAppearance = new XYChart.Series();
        seriesAppearance.setName("Appearance");
        XYChart.Series seriesFlavor = new XYChart.Series();
        seriesFlavor.setName("Flavor");
        XYChart.Series seriesMounthfell = new XYChart.Series();
        seriesMounthfell.setName("Mounthfell");
        XYChart.Series seriesOverall = new XYChart.Series();
        seriesOverall.setName("Overall");
        for (Beer b : listBeers) {

            List<Avaliation> avaliationB = daoAvaliation.getAvaliationsByBeer(b);
            float aroma = 0;
            float appearance = 0;
            float flavor = 0;
            float mounthfeel = 0;
            float overall = 0;
            for (Avaliation a : avaliationB) {
                aroma += a.getScoreAroma();
                appearance += a.getScoreAppearence();
                flavor += a.getScoreFlavor();
                mounthfeel += a.getScoreMouthfeel();
                overall += a.getScoreOverall();
            }
            int countAvaliation = avaliationB.size();
            if (countAvaliation > 0) {
                seriesAroma.getData().add(new XYChart.Data(b.getName(), aroma / countAvaliation));
                seriesAppearance.getData().add(new XYChart.Data(b.getName(), appearance / countAvaliation));
                seriesFlavor.getData().add(new XYChart.Data(b.getName(), flavor / countAvaliation));
                seriesMounthfell.getData().add(new XYChart.Data(b.getName(), mounthfeel / countAvaliation));
                seriesOverall.getData().add(new XYChart.Data(b.getName(), overall / countAvaliation));
            } else {
                seriesAroma.getData().add(new XYChart.Data(b.getName(), 0));
                seriesAppearance.getData().add(new XYChart.Data(b.getName(), 0));
                seriesFlavor.getData().add(new XYChart.Data(b.getName(), 0));
                seriesMounthfell.getData().add(new XYChart.Data(b.getName(), 0));
                seriesOverall.getData().add(new XYChart.Data(b.getName(), 0));
            }
        }
        barChart.getData().addAll(seriesAroma, seriesAppearance, seriesFlavor, seriesMounthfell, seriesOverall);

        try {
            FileInputStream cancelImage = new FileInputStream(System.getProperty("user.dir") + "\\src\\fx\\css\\images\\cancel.png");
            Image imageCancel = new Image(cancelImage);
            btnCancel.setGraphic(new ImageView(imageCancel));
        } catch (FileNotFoundException ex) {
            System.out.println("Image error");
        }
    }

    @FXML
    void cancel(ActionEvent event) {
        MenuController menu;
        try {
            menu = (MenuController) application.replaceSceneContent("menu/menu.fxml", Home.MENUWIDTH, Home.MENUHEIGTH, "BJCP-Scoresheet");
            menu.setMessage("sucess", "Welcome " + Home.loggedUser.getName());
            menu.setApp(application);
        } catch (Exception ex) {
            Logger.getLogger(JudgeController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setApp(Home application) {
        this.application = application;
    }

}
